package com.ys.controller;

import com.ys.entity.Stu;
import com.ys.service.StuService;
import com.ys.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName LoginController
 * @Author 1628994918qq.com
 * @Date 2023/2/28 20:17
 */
@Slf4j
@RestController
public class LoginController {

    @Autowired
    private StuService stuService;

    @Autowired
    private JWTUtils jwtUtils;

    @RequestMapping("/login")
    public Map<String, Object> login(@RequestBody Stu stu) {
        Map<String, Object> map = new HashMap<>();
        Stu stu1 = stuService.selectStuByStuId(stu.getStuId());
        if (stu.getStuId().equals(stu1.getStuId()) && stu.getStuPassword().equals(stu1.getStuPassword())) {
            String token = jwtUtils.createJWT(stu);
            log.info("JWT生成的token：[{}]",token);
            map.put("token", token);
            map.put("status",true);
            map.put("msg","登录成功");
            return map;
        }
        return null;
    }

    @RequestMapping("/test/test")
    public String test(){
        log.info("输出：[{}]","你好，yanshuai");
        return "测试成功";
    }

    @RequestMapping("/test/upFile")
    public String upFile(MultipartFile file) throws IOException {
        log.info("输出：[{}]",file.getName());
        byte[] bytes = file.getBytes();
        log.info("文件转为baye数组：[{}]",bytes);
        return "测试成功";
    }
}
